﻿namespace BM.Tools.WebControls.MForm.Controls.Modifiers
{
    using System;
    using System.Linq;
    using System.Web.UI;

    using BM.Tools.WebControls.MForm.Controls.Additions;
    using BM.Tools.WebControls.MForm.Controls.Items;
    using BM.Tools.WebControls.MForm.Controls.Modifiers.Additions;
    using BM.Tools.WebControls.MForm.Controls.Tabs;
    using BM.Tools.WebControls.MForm.Navigation;

    /// <summary>
    /// A class that adds Tab container and tabs for the items it is bound to.
    /// </summary>
    public class TabbedDisplayModifier : OffspringsOwnerModifier
    {
        /// <summary>
        /// Gets or sets tab container id in which the tabs should appear.
        /// </summary>
        /// <value>
        /// The tab container id.
        /// </value>
        public string TabContainerId
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the selected tab index.
        /// </summary>        
        public int SelectedTabIndex
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the client side tab created event
        /// </summary>
        public string OnClientTabCreated 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// hides the item's title and creates a tab for the item.
        /// </summary>
        /// <param name="offspringItem">
        /// The offspring item.
        /// </param>
        /// <param name="e">
        /// The event args.
        /// </param>
        protected override void OnItemPreRender(OffspringsOwnerItem offspringItem, EventArgs e)
        {
            offspringItem.Additions.Add(CreateAddition());
            offspringItem.Title.Visible = false;

            if (offspringItem.IsPrototypeDescendant())
            {
                return;
            }

            AddTab(offspringItem);
        }

        private static TabContainer GetContainer(OffspringsOwner offspring, string containerId)
        {
            if (string.IsNullOrEmpty(containerId))
            {
                throw new ArgumentException("The tab container id cannot be null or empty");
            }

            Control parent = offspring;
            do
            {
                parent = parent.NamingContainer;
                var container = parent.FindControl(containerId) as TabContainer;
                if (container != null)
                {
                    return container;
                }
            } 
            while (parent != offspring.ParentItem);

            throw new InvalidOperationException("Cannot find tab container with id: " + containerId);            
        }

        private void AddTab(OffspringsOwnerItem offspringItem)
        {
            var container = GetContainer(offspringItem.Basis, TabContainerId);
            if (!offspringItem.IsPrototypeItem)
            {
                if (!container.Tabs.Any(tab => tab.TargetControl.ClientID == offspringItem.ClientID))
                {
                    container.Tabs.Add(new FormTab(offspringItem));
                }
            }

            container.SelectedTabIndex = SelectedTabIndex;
        }

        /// <summary>
        /// creates a TabbedDisplayAddition for the item
        /// </summary>
        /// <returns>
        /// the tabbed display addition
        /// </returns>
        private Addition CreateAddition()
        {
            return new TabbedDisplayAddition(this);
        }        
    }
}